package com.zros.modules.system.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zros.common.api.vo.PageResult;
import com.zros.common.api.vo.Result;
import com.zros.common.api.vo.Select;
import com.zros.modules.system.dto.CreateRole;
import com.zros.modules.system.dto.UpdateRole;
import com.zros.modules.system.dto.UpdateRoleDataResource;
import com.zros.modules.system.dto.UpdateRoleResource;
import com.zros.modules.system.entity.Role;
import com.zros.modules.system.entity.RoleDataResource;
import com.zros.modules.system.entity.RoleResource;
import com.zros.modules.system.service.IRoleDataResourceService;
import com.zros.modules.system.service.IRoleResourceService;
import com.zros.modules.system.service.IRoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: RoleController
 * @Description: Role前端控制器
 * 
 * @date 2018年5月18日 下午4:06:17
 */
@RestController
@RequestMapping("/role")
@Api(value = "RoleController|角色相关的前端控制器")
public class RoleController {

	@Autowired
	private IRoleService roleService;

	@Autowired
	private IRoleResourceService roleResourceService;
	
	@Autowired
	private IRoleDataResourceService roleDataResourceService;

	/**
	 * 查询角色列表
	 */
	@GetMapping("/list")
	//@RequiresRoles("SYSADMIN")
	@ApiOperation(value = "查询角色列表")
	public PageResult<Role> list(Role role, Page<Role> page) {
		Page<Role> pageRole = roleService.selectRoleList(page, role);
		PageResult<Role> pageResult = new PageResult<Role>(pageRole.getTotal(), pageRole.getRecords());
		return pageResult;
	}

	/**
	 * 添加角色
	 */
	@PostMapping("/create")
	//@RequiresRoles("SYSADMIN")
	@ApiOperation(value = "添加角色")
	/* @AroundLog(name = "添加角色") */
	public Result<?> create(@RequestBody CreateRole role) {
		boolean result = roleService.createRole(role);
		if (result) {
			return new Result<>().success("添加成功");
		} else {
			return new Result<>().error("添加失败，请重试");
		}
	}

	/**
	 * 修改角色
	 */
	@PostMapping("/update")
	//@RequiresRoles("SYSADMIN")
	@ApiOperation(value = "更新角色")
	/* @AroundLog(name = "更新角色") */
	public Result<?> update(@RequestBody UpdateRole role) {
		boolean result = roleService.updateRole(role);
		if (result) {
			return new Result<>().success("修改成功");
		} else {
			return new Result<>().error("修改失败");
		}
	}

	/**
	 * 删除角色
	 */
	//@RequiresRoles("SYSADMIN")
	@PostMapping("/delete/{roleId}")
	@ApiOperation(value = "删除角色")
	/* @AroundLog(name = "删除角色") */
	@ApiImplicitParam(paramType = "path", name = "roleId", value = "角色ID", required = true, dataType = "Long")
	public Result<?> delete(@PathVariable("roleId") Long roleId) {
		if (null == roleId) {
			return new Result<>().error("ID不能为空");
		}
		boolean result = roleService.deleteRole(roleId);
		if (result) {
			return new Result<>().success("删除成功");
		} else {
			return new Result<>().error("删除失败");
		}
	}

	/**
	 * 修改角色状态
	 */
	@PostMapping("/status/{roleId}/{roleStatus}")
	//@RequiresRoles("SYSADMIN")
	@ApiOperation(value = "修改角色状态")
	/* @AroundLog(name = "修改角色状态") */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "Long", paramType = "path"),
			@ApiImplicitParam(name = "roleStatus", value = "角色状态", required = true, dataType = "String", paramType = "path") })
	public Result<?> updateStatus(@PathVariable("roleId") Long roleId,
			@PathVariable("roleStatus") String roleStatus) {
		if (null == roleId || StringUtils.isEmpty(roleStatus)) {
			return new Result<>().error("ID和状态不能为空");
		}
		UpdateRole role = new UpdateRole();
		role.setId(roleId);
		role.setRoleStatus(roleStatus);
		boolean result = roleService.updateRole(role);
		if (result) {
			return new Result<>().success("修改成功");
		} else {
			return new Result<>().error("修改失败");
		}
	}

	/**
	 * 获取角色资源
	 * 
	 * @param roleId
	 * @return
	 */
	@GetMapping(value = "/resource/{roleId}")
	//@RequiresRoles("SYSADMIN")
	@ApiOperation(value = "获取角色的权限资源")
	@ApiImplicitParam(paramType = "path", name = "roleId", value = "角色ID", required = true, dataType = "Long")
	public Result<List<RoleResource>> queryRoleResource(@PathVariable("roleId") Long roleId) {
		QueryWrapper<RoleResource> ew = new QueryWrapper<>();
		ew.eq("role_id", roleId);
		List<RoleResource> list = roleResourceService.list(ew);
		return new Result<List<RoleResource>>().success().put(list);
	}

	/**
	 * 获取角色数据资源
	 * 
	 * @param roleId
	 * @return
	 */
	@GetMapping(value = "/dataresource/{roleId}")
	//@RequiresRoles("SYSADMIN")
	@ApiOperation(value = "获取角色的数据资源")
	@ApiImplicitParam(paramType = "path", name = "roleId", value = "角色ID", required = true, dataType = "Long")
	public Result<List<RoleDataResource>> queryRoleDataResource(@PathVariable("roleId") Long roleId) {

		QueryWrapper<RoleDataResource> ew = new QueryWrapper<>();
		ew.eq("role_id", roleId);
		List<RoleDataResource> list = roleDataResourceService.list(ew);
		return new Result<List<RoleDataResource>>().success().put(list);
		
	}

	/**
	 * 修改角色资源
	 * 
	 * @param updateRoleResource
	 * @return
	 */
	@PostMapping(value = "/resource/update")
	//@RequiresRoles("SYSADMIN")
	@ApiOperation(value = "修改角色的权限资源")
	/* @AroundLog(name = "修改角色的权限资源") */
	public Result<?> updateRoleResource(@RequestBody UpdateRoleResource updateRoleResource) {
		boolean result = roleResourceService.updateList(updateRoleResource);
		if (result) {
			return new Result<>().success("修改成功");
		} else {
			return new Result<>().error("修改失败");
		}
	}
	
	/**
	 * 修改角色的数据权限
	 * 
	 * @param updateRoleDataResource
	 * @return
	 */
	@PostMapping(value = "/dataresource/update")
	//@RequiresRoles("SYSADMIN")
	@ApiOperation(value = "修改角色的数据权限")
	/* @AroundLog(name = "修改角色的权限资源") */
	public Result<?> updateRoleDataResource(@RequestBody UpdateRoleDataResource updateRoleDataResource) {
		boolean result = roleDataResourceService.updateList(updateRoleDataResource);
		if (result) {
			return new Result<>().success("修改成功");
		} else {
			return new Result<>().error("修改失败");
		}
	}
	

	/**
	 * 查询所有角色列表
	 * 
	 * @return
	 */
	@GetMapping(value = "/all")
	//@RequiresAuthentication
	@ApiOperation(value = "查询所有角色列表")
	public Result<List<Select>> queryAll() {
		QueryWrapper<Role> ew = new QueryWrapper<>();
		ew.eq("role_status", "1");
		List<Role> result = roleService.list(ew);
		List<Select> selectList = new ArrayList<Select>();
		for (Role role : result) {
			Select select = new Select();
			select.setLabel(String.valueOf(role.getRoleName()));
			select.setValue(String.valueOf(role.getId()));
			selectList.add(select);
		}
		
		return new Result<List<Select>>().success().put(selectList);
	}
}
